#!/bin/bash
# description:　Rsync war file
# version:	1.0
# author:	zhangyanbin
# contact:	yanbin_zhang@foxitsoftware.com
#
. /vagrant/vagrant/functions.sh

# "master" or "develop"
usage()
 {
	echo "usage: `basename $0` \$1 \$2"
	echo "\$1 :specfify a branch (develop/master)"
	echo "\$2 :(Optional parameters)specfify a deploy name (platform/worker/cas/cmis/restheart/webapp)"
}

if [ -z $1 ]; then
#echo "ERROR: please specfify a branch (develop/master)"
	usage
	exit -1
fi

BRANCH=$1

if [ "$DL_SERVER" = "" ]; then
error_and_log "Download server url is empty. Please check $DL_SERVER_FILE_NAME"
exit -1;
else
warning_and_log "Download Server Url is: $DL_SERVER"
fi

GENERIC_SRC=smx@${DL_SERVER}:/opt/www/ops30.sumilux.com


if [ "$2" = "platform" -o "$2" = "worker" ]||[ -z $2 ]; then
echo "--> Begin to download platform/worker war file, branch = $BRANCH "
 if [ ! -e /opt/pcs-server/pcs-server ]; then
mkdir -p /opt/pcs-server/pcs-server
 fi
WAR_FILE_SRC=smx@${DL_SERVER}:/opt/www/${DL_SERVER}/pcs-server/latest/${BRANCH}/
WAR_FILE_DST=/opt/pcs-server/pcs-server/
rsync ${RSYNC_OPTS} -e "${RSH_OPTS}" ${WAR_FILE_SRC} ${WAR_FILE_DST} 1>/dev/null

 if [ ! -e /opt/pcs-server/pcs-server-api ]; then
mkdir -p /opt/pcs-server/pcs-server-api
 fi
WAR_FILE_SRC=smx@${DL_SERVER}:/opt/www/${DL_SERVER}/pcs-api/latest/${BRANCH}/
WAR_FILE_DST=/opt/pcs-server/pcs-server-api/
rsync ${RSYNC_OPTS} -e "${RSH_OPTS}" ${WAR_FILE_SRC} ${WAR_FILE_DST} 1>/dev/null

echo "--> Begin to download so file,branch = $BRANCH "
SOFILES_SRC=smx@${DL_SERVER}:/opt/www/${DL_SERVER}/sofiles_64/latest/${BRANCH}/
rsync ${RSYNC_OPTS} -e "${RSH_OPTS}" ${SOFILES_SRC} /tmp/sofiles_64/ 1>/dev/null
#sudo \cp /tmp/sofiles_64/PDFLinearize_x64.so /usr/share/tomcat/latest/temp/PDFLinearize_x64.so
sudo \cp /tmp/sofiles_64/libPdfEngine_x64.so /usr/share/tomcat/latest/temp/v2/libPdfEngine_x64.so
fi

if [ -z $2 ]; then
echo "--> Begin to download selenium file"
rsync ${RSYNC_OPTS} -e "${RSH_OPTS}" ${GENERIC_SRC}/selenium /tmp/ 1>/dev/null

echo "--> Begin to download nodejs package"
 if [ ! -e /tmp/vagrant-cache/wget ]; then
mkdir -p /tmp/vagrant-cache/wget
 fi

 if [ ! -e /tmp/vagrant-cache/wget/node-v4.2.2-linux-x64.tar.gz ]; then 
rsync ${RSYNC_OPTS} -e "${RSH_OPTS}" ${GENERIC_SRC}/nodejs /tmp/vagrant-cache/wget/ 1>/dev/null
 fi
fi

if [ "$2" = "platform" -o "$2" = "worker" -o "$2" = "cas" -o "$2" = "cmis" -o "$2" = "restheart" ]||[ -z $2 ]; then
echo "--> Begin to download jdk1.8 package"
 if [ ! -e /tmp/vagrant-cache/wget/jdk1.8/jdk-8u45-linux-x64.rpm ]; then
rsync ${RSYNC_OPTS} -e "${RSH_OPTS}" ${GENERIC_SRC}/jdk1.8 /tmp/vagrant-cache/wget/ 1>/dev/null
 fi
fi

if [[ $2 = "cas" ]]||[[ -z $2 ]]; then
echo "--> Begin to download pcs-cas war file,branch = $BRANCH "
 if [ ! -e /opt/pcs-cas/pcs-cas ]; then
mkdir -p /opt/pcs-cas/pcs-cas
 fi

CASWAR_FILE_SRC=smx@${DL_SERVER}:/opt/www/${DL_SERVER}/sso-server/latest/${BRANCH}/
CASWAR_FILE_DST=/opt/pcs-cas/pcs-cas/
rsync ${RSYNC_OPTS} -e "${RSH_OPTS}" ${CASWAR_FILE_SRC} ${CASWAR_FILE_DST} 1>/dev/null
fi
if [[ $2 = "restheart" ]]||[[ -z $2 ]]; then
echo "--> Begin to download restheart war file"
 if [ ! -e /opt/restheart ]; then
mkdir -p /opt/restheart
 fi

RESTHEART_FILE_SRC=smx@${DL_SERVER}:/opt/www/${DL_SERVER}/restheart/latest/
RESTHEART_FILE_DST=/opt/restheart
rsync ${RSYNC_OPTS} -e "${RSH_OPTS}" ${RESTHEART_FILE_SRC} ${RESTHEART_FILE_DST} 1>/dev/null
fi

if [[ $2 = "cmis" ]]||[[ -z $2 ]];then
echo "--> Begin to download CMIS war file,branch = $BRANCH "
if [ ! -e /opt/cmis-server ]; then
mkdir -p /opt/cmis-server
fi

CMIS_FILE_SRC=smx@${DL_SERVER}:/opt/www/${DL_SERVER}/cmis-server/latest/${BRANCH}/
CMIS_FILE_DST=/opt/cmis-server/
rsync ${RSYNC_OPTS} -e "${RSH_OPTS}" ${CMIS_FILE_SRC} ${CMIS_FILE_DST} 1>/dev/null

 if [ ! -e /var/lib/tomcat/webapps ]; then
mkdir -p /var/lib/tomcat/webapps
 fi

sudo \cp /opt/cmis-server/chemistry-opencmis-server-jcr-1.0.0-SNAPSHOT.war /var/lib/tomcat/webapps/cmis.war
fi

if [ -z $2 ];then
echo "--> Begin to download jetty-run jar file"
if [ ! -e /opt/jetty-run ]; then
mkdir -p /opt/jetty-run
fi
JETTY_FILE_SRC=smx@${DL_SERVER}:/opt/www/${DL_SERVER}/jetty-run/latest/
JETTY_FILE_DST=/opt/jetty-run/
rsync -av -e "${RSH_OPTS}" ${JETTY_FILE_SRC} ${JETTY_FILE_DST} 1>/dev/null
fi

if [[ $2 = "webapp" ]]||[[ -z $2 ]];then
echo "--> Begin to download webpdf fonts file"
 if [ ! -e /opt/webreader/share/fonts ]; then
mkdir -p /opt/webreader/share/fonts
 fi

FONTS_FILE_SRC=${GENERIC_SRC}/webreader/share/
FONTS_FILE_DST=/opt/webreader/share/
rsync ${RSYNC_OPTS} -e "${RSH_OPTS}" ${FONTS_FILE_SRC} ${FONTS_FILE_DST} 1>/dev/null
fi

echo "--> download_files.sh finished in ${SECONDS}s"
